package com.module.content.ui.info

import android.graphics.drawable.AnimationDrawable
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.allen.app.core.RouterHub
import com.allen.app.data.bean.FRAGMENT_ID_INFO
import com.allen.app.data.event.UpdateMainEvent
import com.allen.base.base.adapter.BaseFragmentPageAdapter
import com.allen.base.base.fragment.BaseFragment
import com.allen.base.utils.AppUtils
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.thread.EventThread
import com.module.content.R
import com.module.content.utils.InfoIndex
import kotlinx.android.synthetic.main.info_fragment_index.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * author : Allen
 * date   : 2020/08/18
 * time   : 17:04
 * desc   : 糗事主页列表
 */
@Route(path = RouterHub.ROUTER_INFO_FRAGMENT_INDEX)
class InfoFragmentIndex : BaseFragment() {
    override fun getLayoutResID(): Int = R.layout.info_fragment_index
    private var timeTask: TimerTask? = null
    override fun isRegisterRxBus() = true
    private val fragments = ArrayList<Fragment>()

    companion object {
        var currentIndex = 1
    }

    override fun initViews() {
        super.initViews()
        fragments.clear()
        fragments.add(InfoFragment.newInstance(InfoIndex.INFO_ATTENTION))
        fragments.add(InfoFragment.newInstance(InfoIndex.INFO_EXCLUSIVE))
        fragments.add(InfoFragment.newInstance(InfoIndex.INFO_VIDEO))
        fragments.add(InfoFragment.newInstance(InfoIndex.INFO_TEXT))
        fragments.add(InfoFragment.newInstance(InfoIndex.INFO_IMAGE))
        fragments.add(InfoFragment.newInstance(InfoIndex.INFO_ESSENCE))
        fragments.add(InfoFragment.newInstance(InfoIndex.INFO_NEWSPAPER))
        val titles = arrayListOf("关注", "专享", "视频", "纯文", "纯图", "精华", "爆社")
        val mAdapter = BaseFragmentPageAdapter(childFragmentManager, fragments, titles)
        mViewPager.adapter = mAdapter
        mTabLayout.setViewPager(mViewPager)
        mViewPager.currentItem = currentIndex
    }

    override fun initEvents() {
        super.initEvents()
        mViewPager?.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                getCurrentFragment(currentIndex)?.autoStop()
                currentIndex = position
                getCurrentFragment(position)?.autoPlay()
            }
        })
    }

    @Subscribe(thread = EventThread.MAIN_THREAD)
    fun onMessageEvent(messageEvent: UpdateMainEvent) {
        //糗事菜单点击刷新
        if (messageEvent.index == FRAGMENT_ID_INFO) {
            getCurrentFragment(currentIndex)?.autoRefresh()
        }
    }

    /**
     * 获取当前选择的糗事页面
     */
    private fun getCurrentFragment(index: Int): InfoFragment? {
        if (index < fragments.size) {
            val f = fragments[index]
            if (f is InfoFragment) {
                return f
            }
        }
        return null
    }

    /**
     * 计时器5s钟 播放一次签到动画
     */
    private fun startTime() {
        timeTask?.cancel()
        timeTask = object : TimerTask() {
            override fun run() {
                startSignAnim()
            }
        }
        Timer().scheduleAtFixedRate(timeTask, 0, 5000)
    }

    /**
     * 启动签到动画
     */
    private fun startSignAnim() {
        AppUtils.post(Runnable {
            val bg = signIv?.background
            if (bg is AnimationDrawable) {
                if (bg.isRunning) {
                    bg.stop()
                }
                bg.start()
            }
        })
    }

    override fun onPause() {
        super.onPause()
        timeTask?.cancel()
        getCurrentFragment(currentIndex)?.autoStop()
    }

    override fun onResume() {
        super.onResume()
        startTime()
        getCurrentFragment(currentIndex)?.autoPlay()
    }
}